/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: LianBao
 * Date: 2024-09-03
 * Time: 20:46
 */
//懒汉模式
public class SingletonLazy {

    private static volatile SingletonLazy instance = null;//加上volatile防止指令重排序

    private SingletonLazy() {

    }

    public static SingletonLazy getInstance() {
        Object locker = new Object();

        if (instance == null) {//如果不为空是不需要加锁的,否则会产生阻塞,这里判断一下是不是需要加锁
            synchronized (locker) {//加锁,if判断和new操作不是原子的,
                if (instance == null) {
                    instance = new SingletonLazy();
                }
            }
        }
        return instance;
    }
}
